package com.hdey.service.heng.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hdey.mapper.heng.RoleMapper;
import com.hdey.mapper.heng.SmbmsRolePermissionMapper;
import com.hdey.mapper.heng.TbUscPermissionMapper;
import com.hdey.pojo.heng.SmbmsRolePermission;
import com.hdey.pojo.heng.TbUscPermission;
import com.hdey.pojo.heng.TbUscRole;
import com.hdey.service.heng.RoleService;
import com.hdey.vo.TreeNode;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;

@Service("roleService")
@Transactional
public class RoleServiceImpl extends ServiceImpl<RoleMapper, TbUscRole> implements RoleService {
    @Resource
    private RoleMapper mapper;
    @Resource
    private TbUscPermissionMapper permissionMapper;
    @Resource
    private SmbmsRolePermissionMapper smbmsRolePermissionMapper;

//    private List<Integer> getPermIdList(Integer roleId){
//        LambdaQueryWrapper<SmbmsRolePermission> wrapper= Wrappers.lambdaQuery();
//        wrapper.eq(SmbmsRolePermission::getRoleId,roleId);
//        return smbmsRolePermissionMapper.findPermIdList(wrapper);
////        return smbmsRolePermissionMapper.findPermIdList(roleId);
//    }

    @Override
    public List<TbUscRole> showAll() {
        return mapper.showAll();
    }


    @Override
    public Page<TbUscRole> select(Integer current, Integer size, String roleName) {
        Page<TbUscRole> page=new Page<>(current,size);
        LambdaQueryWrapper<TbUscRole> wrapper=new LambdaQueryWrapper<>();
        if(roleName!=null&&!"".equals(roleName)&&!"null".equals(roleName)){
            wrapper.like(TbUscRole::getRoleName,roleName);
        }
        mapper.selectPage(page, wrapper);
        return page;
    }

    @Override
    public TbUscRole selectById(Integer roleId) {
        return mapper.selectById(roleId);
    }

    @Override
    public int update(TbUscRole tbUscRole) {
        return mapper.updateById(tbUscRole);
    }

    @Override
    public void grant(Integer roleId,Integer[] ids) {
        smbmsRolePermissionMapper.delete(Wrappers.<SmbmsRolePermission>update().eq("role_id",roleId));
        if (ids!=null&&ids.length>0){
            for (Integer id:ids){
                smbmsRolePermissionMapper.insert(new SmbmsRolePermission(roleId,id));
            }
        }
    }

//    private TreeNode getTreeNode(Integer roleId,TbUscPermission tbUscPermission){
//        TreeNode treeNode=new TreeNode();
//        treeNode.setId(tbUscPermission.getId());
//        treeNode.setTitle(tbUscPermission.getName());
//        List<Integer> list=smbmsRolePermissionMapper.findPermIdList(roleId);
//        if (list!=null&&list.contains(tbUscPermission.getId())){
//            treeNode.setChecked(true);
//        }
//        return treeNode;
//    }

    private List<TreeNode> searchByParentId(Integer parentId,Integer roleId){

            LambdaQueryWrapper<TbUscPermission> wrapper=new LambdaQueryWrapper<>();
            if (parentId==null){
                wrapper.isNull(TbUscPermission::getParentId);
            }else {
                wrapper.eq(TbUscPermission::getParentId,parentId);
            }
        List<Integer> permIdList=smbmsRolePermissionMapper.findPermIdList(roleId);
            List<TbUscPermission> list = permissionMapper.selectList(wrapper);
            return list.stream().map(TbUscPermission->{
                TreeNode treeNode=new TreeNode();
                treeNode.setId(TbUscPermission.getId());
                treeNode.setTitle(TbUscPermission.getName());

                Subject currentUser = SecurityUtils.getSubject();
                boolean permitted = currentUser.isPermitted(TbUscPermission.getPermCode());

                    treeNode.setChecked(permitted);
                return treeNode;
            }).collect(Collectors.toList());

    }

    @Override
    public List<TreeNode> searchTreeData(Integer roleId) {
        List<TreeNode> menuList01=this.searchByParentId(null,roleId);
        for (TreeNode node01:menuList01){
            List<TreeNode> menuList02=this.searchByParentId(node01.getId(),roleId);
            node01.setChildren(menuList02);
            for (TreeNode node02:menuList02){
                List<TreeNode> btnList=this.searchByParentId(node02.getId(),roleId);
                node02.setChildren(btnList);
            }
        }
        return menuList01;
    }

    @Override
    public int add(TbUscRole tbUscRole) {
        tbUscRole.setCreateBy("张三");
        tbUscRole.setCreateDate(LocalDate.now());
        tbUscRole.setDeleteFlag("0");
        return mapper.insert(tbUscRole);
    }

    @Override
    public String getLastroleCode() {
        return mapper.getLastroleCode();
    }

    @Override
    public int del(Integer roleId) {
        return mapper.deleteById(roleId);
    }

    @Override
    public String showroleName(Integer roleId) {
        return mapper.showroleName(roleId);
    }
}
